package com.ymatou.iapi.userservice.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.login.service.LoginAuthCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userservice.parameter.UpdateLoginPwdBean;
import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymatou.iapi.userservice.service.UpdateLoginPwdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.utils.TestDataManager;

/**
 * 更新登录密码Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_UpdateLoginPwd {
	private static UpdateLoginPwdBean updateloginpwdBean;
	private static UpdateLoginPwdCall updateloginpwdCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新登录密码Test");
	}

	@Before
	public void caseUp() {
		updateloginpwdBean = new UpdateLoginPwdBean();
		updateloginpwdCall = new UpdateLoginPwdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateloginpwdCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateLoginPwd_001() {
		Logger.start(true, "更新登录密码,成功");
		try {
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			Integer uid = JUserCallService.UserRegisterInfoRequestDtoCall(urb).getUserid();
			String oldLoginPassword = urb.getPas();
			String newLoginPassword = "abc123";

			updateloginpwdBean.setOldPassword(oldLoginPassword);
			updateloginpwdBean.setNewPassword(newLoginPassword);
			updateloginpwdBean.setUserId(uid);
			updateloginpwdCall.setData(updateloginpwdBean);
			updateloginpwdCall.callService();

			Logger.verifyEquals("true", updateloginpwdCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", updateloginpwdCall.getString("ResponseCode"), "验证ResponseCode");
			// 验证登录密码是否修改正确，再次修改成功则修改正确，否则失败
			String newLoginPassword2 = "123abc";
			updateloginpwdBean.setOldPassword(newLoginPassword);
			updateloginpwdBean.setNewPassword(newLoginPassword2);
			updateloginpwdBean.setUserId(uid);
			updateloginpwdCall.setData(updateloginpwdBean);
			updateloginpwdCall.callService();
			LoginAuthCall logincall = JUserCallService.login(urb.getLoginId(), newLoginPassword2,"App", urb.getClientIp(),urb.getDeviceId());
			Logger.verifyEquals("true", logincall.getString("Success"), "验证login Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateLoginPwd_002() {
		Logger.start(true, "新密码为空,更新失败");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);
			String oldLoginPassword = cur.getLoginPassword();
			// String newLoginPassword = TestDataManager.getRandomNum(100);

			updateloginpwdBean.setOldPassword(oldLoginPassword);
			updateloginpwdBean.setNewPassword(null);
			updateloginpwdBean.setUserId(uid);
			updateloginpwdCall.setData(updateloginpwdBean);
			updateloginpwdCall.callService();

			Logger.verifyEquals("false", updateloginpwdCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100", updateloginpwdCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("不能为空", updateloginpwdCall.getString("LastErrorMessage"), "验证LastErrorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateLoginPwd_003() {
		Logger.start(true, "旧密码错误（不匹配）,更新失败");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);
			String oldLoginPassword = cur.getLoginPassword();
			String newLoginPassword = TestDataManager.getRandomNum(6);

			updateloginpwdBean.setOldPassword(oldLoginPassword + "1");
			updateloginpwdBean.setNewPassword(newLoginPassword);
			updateloginpwdBean.setUserId(uid);
			updateloginpwdCall.setData(updateloginpwdBean);
			updateloginpwdCall.callService();

			Logger.verifyEquals("false", updateloginpwdCall.getString("Success"), "验证Success");
			Logger.verifyEquals("300008", updateloginpwdCall.getString("ResponseCode"), "验证ResponseCode");
			// Logger.verifyIsContains("请求参数非法",
			// updateloginpwdCall.getString("LastErrorMessage"),
			// "验证LastErrorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateLoginPwd_004() {
		Logger.start(true, "旧密码为空,重置密码成功");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);
			String oldLoginPassword = cur.getLoginPassword();
			String newLoginPassword = TestDataManager.getRandomNum(6);

			updateloginpwdBean.setOldPassword(null);
			updateloginpwdBean.setNewPassword(newLoginPassword);
			updateloginpwdBean.setUserId(uid);
			updateloginpwdCall.setData(updateloginpwdBean);
			updateloginpwdCall.callService();

			Logger.verifyEquals("true", updateloginpwdCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", updateloginpwdCall.getString("ResponseCode"), "验证ResponseCode");
			// Logger.verifyIsContains("请求参数非法",
			// updateloginpwdCall.getString("LastErrorMessage"),
			// "验证LastErrorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateLoginPwd_005() {
		Logger.start(true, "userid为空,更新失败");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);
			String oldLoginPassword = cur.getLoginPassword();
			String newLoginPassword = TestDataManager.getRandomNum(6);

			updateloginpwdBean.setOldPassword(oldLoginPassword);
			updateloginpwdBean.setNewPassword(newLoginPassword);
			updateloginpwdBean.setUserId(null);
			updateloginpwdCall.setData(updateloginpwdBean);
			updateloginpwdCall.callService();

			Logger.verifyEquals("false", updateloginpwdCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100", updateloginpwdCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("未提供", updateloginpwdCall.getString("LastErrorMessage"), "验证LastErrorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}